package com.company.woye.Inventory.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.company.woye.Inventory.entity.SummarzingEntity;
import com.company.woye.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class InventoryListviewAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private Context mContext;
    private List<SummarzingEntity> list;
    private OnItemClick onItemClick;

    public InventoryListviewAdapter(Context context, List<SummarzingEntity> list,OnItemClick onItemClick) {
        this.inflater = LayoutInflater.from(context);
        this.mContext = context;
        this.list = list;
        this.onItemClick=onItemClick;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHodler = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_adapter_inventory, parent, false);
            viewHodler = new ViewHolder(convertView);
            convertView.setTag(viewHodler);
        } else {
            viewHodler = (ViewHolder) convertView.getTag();
        }
        if (list.size() > 0) {
            SummarzingEntity summarzingEntity = list.get(position);
            viewHodler.tvInventoryItemBoxinpacknum.setText(summarzingEntity.getBoxInPackNum());
            viewHodler.tvInventoryItemBoxno.setText(summarzingEntity.getBoxNo());
            viewHodler.tvInventoryItemContinutetime.setText(summarzingEntity.getContinuedTimes());
            viewHodler.tvInventoryItemDBH.setText(summarzingEntity.getDBH());
            viewHodler.tvInventoryItemInputdate.setText(summarzingEntity.getInputDateTime());
            viewHodler.tvInventoryItemMaterielType.setText(summarzingEntity.getMaterielType());
            viewHodler.tvInventoryItemOrderNo.setText(summarzingEntity.getOrderNo());
            viewHodler.tvInventoryItemOutState.setText(summarzingEntity.getOutState());
            viewHodler.tvInventoryItemPlaceNo.setText(summarzingEntity.getPlaceNo());
            viewHodler.tvInventoryItemPO.setText(summarzingEntity.getPO());
            viewHodler.tvInventoryItemSize.setText(summarzingEntity.getSize());
            viewHodler.tvInventoryItemStockVolume.setText(summarzingEntity.getStockVolume());
            viewHodler.tvInventoryItemStyle.setText(summarzingEntity.getStyleNo());
            viewHodler.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (b) {
                        summarzingEntity.setIsChoose(true);
                    } else {
                        summarzingEntity.setIsChoose(false);
                    }
                    notifyDataSetChanged();
                }
            });
            viewHodler.cb.setChecked(summarzingEntity.getIsChoose());

            viewHodler.tvInventoryItemOutState.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (!TextUtils.isEmpty(summarzingEntity.getOutState())) {
                        if (summarzingEntity.getOutState().equals("待出库")) {
                            onItemClick.OueState(position);
                        }
                    }
                }
            });

        }

        return convertView;
    }


    static
    class ViewHolder {
        @BindView(R.id.tv_inventory_item_style)
        TextView tvInventoryItemStyle;
        @BindView(R.id.tv_inventory_item_MaterielType)
        TextView tvInventoryItemMaterielType;
        @BindView(R.id.tv_inventory_item_size)
        TextView tvInventoryItemSize;
        @BindView(R.id.tv_inventory_item_orderNo)
        TextView tvInventoryItemOrderNo;
        @BindView(R.id.tv_inventory_item_PO)
        TextView tvInventoryItemPO;
        @BindView(R.id.tv_inventory_item_DBH)
        TextView tvInventoryItemDBH;
        @BindView(R.id.tv_inventory_item_boxno)
        TextView tvInventoryItemBoxno;
        @BindView(R.id.tv_inventory_item_boxinpacknum)
        TextView tvInventoryItemBoxinpacknum;
        @BindView(R.id.tv_inventory_item_place_no)
        TextView tvInventoryItemPlaceNo;
        @BindView(R.id.tv_inventory_item_inputdate)
        TextView tvInventoryItemInputdate;
        @BindView(R.id.tv_inventory_item_continutetime)
        TextView tvInventoryItemContinutetime;
        @BindView(R.id.tv_inventory_item_stock_volume)
        TextView tvInventoryItemStockVolume;
        @BindView(R.id.tv_inventory_item_out_state)
        TextView tvInventoryItemOutState;
        @BindView(R.id.cb)
        CheckBox cb;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public  interface OnItemClick {
        void OueState(int position);
    }

}
